import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.BufferedImage;

import java.util.LinkedList;

import javax.swing.JPanel;


public class DrawGame extends JPanel  {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//draw board and game
	int stoneSize=30;
	Board game;

	Shape savePaint;

	public DrawGame(Board game) {
		this.game=game;


	}



	public void RotationAnimation(int nbrrotation){
		Graphics g = this.getGraphics();


		Graphics2D g2d = (Graphics2D) g;
		Image offscreen = createImage(1000,1000); 
		Graphics2D bufferedImage=(Graphics2D) offscreen.getGraphics();
		//Graphics2D bufferedImage = (Graphics2D)getGraphics().create();
	
	
		//	Shape board= g2d. ;
		g.setColor(new Color(0xffffff));
		g.fillRect(0, 0, 1000, 1000);
		g.setColor(new Color(0x0000));
		double angle = nbrrotation*Math.PI/4;

		for(double i=0;i<angle-0.05;i+=0.05){
		
			bufferedImage.rotate(0.05,game.getPlace(120).x,game.getPlace(120).y);
			//	paintComponent(g);
			paintComponent(bufferedImage);
			  g.drawImage(offscreen,0,0,this); 
			try {Thread.sleep(3);} catch (InterruptedException e) {System.out.println("erreur thread");}
		}


	

		this.repaint();

	}


	/***
	 * repaint the entire board game
	 */
	public void paintComponent(Graphics g) {
		g.setColor(new Color(0xffffff));

		g.fillRect(0, 0, 1000, 1000);
		g.setColor(new Color(0x0));
		g.fillRect(0, 0, 100, 1000);

		Graphics2D g2d= (Graphics2D) g;


		//draw places of the board
		for (int i=0;i<121;i++){	
			g2d.setColor(new Color(0x2aaa));
			g2d.fillOval(game.getPlace(i).x-stoneSize/2,game.getPlace(i).y-stoneSize/2,stoneSize,stoneSize);
			g2d.setColor(new Color(0xffffff));
			stoneSize-=2;
			g2d.fillOval(game.getPlace(i).x-stoneSize/2,game.getPlace(i).y-stoneSize/2,stoneSize,stoneSize);
			stoneSize+=2;
		}



		if(game.selectedStone!=-1){

			LinkedList<Integer> ngb=game.player.GetPossibilities(game.selectedStone);

			g.setColor(game.player.mycolor);

			//g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f ));
			int crossSize=5;
			for(int k=0;k<ngb.size();k++){
				g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f ));
				g2d.fillOval(game.getPlace(ngb.get(k)).x-stoneSize/4,game.getPlace(ngb.get(k)).y-stoneSize/4,stoneSize/2,stoneSize/2);
				g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f ));
				g2d.drawLine(game.getPlace(ngb.get(k)).x-crossSize, game.getPlace(ngb.get(k)).y-crossSize, game.getPlace(ngb.get(k)).x+crossSize, game.getPlace(ngb.get(k)).y+crossSize);
				g2d.drawLine(game.getPlace(ngb.get(k)).x-crossSize, game.getPlace(ngb.get(k)).y+crossSize, game.getPlace(ngb.get(k)).x+crossSize, game.getPlace(ngb.get(k)).y-crossSize);

			}
			//g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f ));
		}

		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f ));


		Player p;
		for (int h=0;h<game.players.size();h++){
			p=game.players.get(h);
			//	System.out.println("player"+p.position);
			for (int i=0;i<10;i++){

				g2d.setColor(p.mycolor);
				if(game.selectedStone==p.getStone(i)){
					g.setColor(p.mycolor.darker().darker().darker());

				}
				//no turn:
				//	g2d.fillOval(game.getPlace(p.getStoneAbs(i)).x-stoneSize/2,game.getPlace(p.getStoneAbs(i)).y-stoneSize/2,stoneSize,stoneSize);			
				//auto turn:
				g2d.fillOval(game.getPlace(p.getStone(i)).x-stoneSize/2,game.getPlace(p.getStone(i)).y-stoneSize/2,stoneSize,stoneSize);

				//	System.out.print(";"+p.getStone(i));
				int d=p.CalculDistanceFin();
				if (d==120){

					g2d.drawString("player "+p.position+" Win!", 5, (p.position+1)*25);
				}else{

					g2d.drawString("player "+p.position+" : "+p.CalculDistanceFin(), 5, (p.position+1)*25);	
				}


			}
			//System.out.println("its objective:"+p.getObjective(0));
		}

		g2d.setColor(new Color(0x000099));

		if(Player.lastmovedest!=-1){
			int	lastmovesrc=Place.rotate_to_position(Player.lastmovesrc,game.player.position);
			int lastmovedest=Place.rotate_to_position(Player.lastmovedest,game.player.position);

			g2d.drawLine(game.getPlace(lastmovedest).x-5, game.getPlace(lastmovedest).y, game.getPlace(lastmovedest).x+5, game.getPlace(lastmovedest).y);
			g2d.drawLine(game.getPlace(lastmovedest).x, game.getPlace(lastmovedest).y-5, game.getPlace(lastmovedest).x, game.getPlace(lastmovedest).y+5);


			g2d.drawLine(game.getPlace(lastmovesrc).x-5, game.getPlace(lastmovesrc).y, game.getPlace(lastmovesrc).x+5, game.getPlace(lastmovesrc).y);
			g2d.drawLine(game.getPlace(lastmovesrc).x, game.getPlace(lastmovesrc).y-5, game.getPlace(lastmovesrc).x, game.getPlace(lastmovesrc).y+5);
		}





		//	g2d.fillOval(game.getPlace(Player.lastmove).x-stoneSize/2,game.getPlace(Player.lastmove).y-stoneSize/2,stoneSize,stoneSize);
		g2d.setColor(new Color(0x000000));

		for (int i=0;i<121;i++){

			//			g2d.drawString(""+game.getPlace(i).position,game.getPlace(i).x-5,game.getPlace(i).y);
			//it's work!

			//	g2d.drawString(""+game.getPlace(i).bx+":"+game.getPlace(i).by,game.getPlace(i).x-7,game.getPlace(i).y+5);
			//	g2d.drawString(""+Board.min_distance_from(0, i),game.places[i].x+5,game.places[i].y+5);
			//interesting value for AI:game.places[i].bx*game.places[i].by
			//	g2d.drawString(""+game.getPlace(i).position,game.getPlace(i).x-5,game.getPlace(i).y);
			//g2d.drawString(""+game.getPlace(i).bx*game.getPlace(i).by,game.getPlace(i).x-5,game.getPlace(i).y+5);
		}


		//	System.out.println(d1.getTime()-new Date().getTime());



	}




}
